﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace mscook.mvc.core
{
    public class PageBreakHelper
    {

        readonly private static int DeaufaltPageSize = 10;
        private int _PageNum;

        public int PageNum
        {
            get { return _PageNum; }
            set
            {
                Start = (value - 1) * PerValue;
                _PageNum = value;
            }
        }

        private int _PerValue;

        public int PerValue
        {
            get { return _PerValue; }
            private set { _PerValue = value; }
        }

        private int _Start;

        public int Start
        {
            get { return _Start; }
            private set { _Start = value; }
        }

        private int _TotalPage;

        public int TotalPage
        {
            get { return _TotalPage; }
            private set { _TotalPage = value; }
        }

        public int GetTotalPage(long total)
        {
            float totalPageNum = (float)total / PerValue;
            TotalPage = totalPageNum > (int)totalPageNum ? (int)totalPageNum + 1 : (int)totalPageNum;
            return TotalPage;
        }

        private void SetTotalPageNum(long total)
        {
            float totalPageNum = (float)total / PerValue;
            TotalPage = totalPageNum > (int)totalPageNum ? (int)totalPageNum + 1 : (int)totalPageNum;
        }

        public int[] GetPageNum(long total)
        {
            SetTotalPageNum(total);
            int mid = PerValue / 2;//
            int midnum = PerValue % 2 == 0 ? mid : mid + 1;//5
            int[] pageArray = new int[PerValue];
            if (TotalPage <= PerValue || PageNum <= midnum)
            {
                if (TotalPage > PerValue)
                {
                    TotalPage = PerValue;
                }
                for (int i = 1; i <= TotalPage; i++)
                {
                    pageArray[i - 1] = i;
                }
            }
            else
            {
                int a;
                if (PageNum >= TotalPage - mid)
                {
                    a = TotalPage - PerValue;
                }
                else
                {
                    a = PageNum - midnum;
                }
                for (int i = 0; i < PerValue; i++)
                {
                    a += 1;
                    pageArray[i] = a;
                }
            }
            return pageArray;
        }

        public void Init(string nowPageNum = "", string nowPerValue = "")
        {
            int pageNum;
            int perValue;
            Int32.TryParse(nowPageNum, out pageNum);
            Int32.TryParse(nowPerValue, out perValue);
            PerValue = perValue < DeaufaltPageSize ? DeaufaltPageSize : perValue;
            PageNum = pageNum <= 0 ? 1 : pageNum;
        }

        public void SetViewDataKeys(ViewDataDictionary ViewData, dynamic ViewBag, long total)
        {
            ViewBag.PageList = GetPageNum(total);
            ViewData[ViewDataKeys.TOTAL_AMOUNT] = total;
            ViewData[ViewDataKeys.TOTAL_PAGE] = GetTotalPage(total);
            ViewData[ViewDataKeys.LIMIT] = PerValue;
            ViewData[ViewDataKeys.CURRENT_PAGE] = PageNum;
            ViewData[ViewDataKeys.PERVALUE] = PerValue;
        }
    }
}